var typescript = require('gulp-tsc');
var gulp = require('gulp');
var babel = require('gulp-babel');
var del = require('del');
var vinylPaths = require('vinyl-paths');
var merge = require('merge-stream');
var path = require('path');
var typedoc = require("gulp-typedoc");
var esdoc = require('gulp-esdoc');
var shell = require('shelljs');

// /** TypeScript的配置 */
var tsCompileOption = {
    target: 'es6',
    tmpDir: "./tempCompile",
    module: "commonjs",
    rootDir: "./ts",
    emitError: false,
    noEmitOnError: true,
    keepTree: false
};
/** Babel的配置 */
var babelOption = {
    optional: ['runtime'],
    // loose: 'all'
    // modules: "common"
};

/** 删除自动生成的ts5,ts6文件 */
gulp.task('del', function () {
    var delEs6 = gulp.src('es6/*')
        .pipe(vinylPaths(del));
    var delEs5 = gulp.src('es5/*')
        .pipe(vinylPaths(del));
    return merge(delEs5, delEs6);
});

/** 动态跟踪ts变化,实时编译到es6 */
gulp.task('watchTs', function () {
    shell.exec('tsc -w', function (status, output) {
        console.log('Exit status:', status);
        console.log('Program output:', output);
    });
});
/** 动态跟踪es6变化,实时编译到es5 */
gulp.task('watchEs6', function () {
    return gulp.watch('./es6/**/*.js', function (event) {
        if (event.type === "added" || event.type === "changed") {
            var relative = path.relative('./es6/', event.path);
            var newPath = path.dirname(path.join('./es5/', relative));
            return gulp.src(event.path)
                .pipe(babel(babelOption))
                .pipe(gulp.dest(newPath));
        }
    });
});

/** 动态编译 */
gulp.task('watch', ['watchEs6', 'watchTs']);

/** 编译ts到es6 */
gulp.task('compileTs', ['del'], function () {
    // return gulp.src('')
    //     .pipe(shell('tsc --target es6 --rootDir ./ts --outDir ./es6 --module commonjs'));
    shell.exec('tsc --target es6 --rootDir ./ts --outDir ./es6 --module commonjs');
});

/** 编译ts到es5*/
gulp.task('compile', ['compileTs'], function () {
    /** 编译es6到es5 */
    return gulp.src('./es6/**/*.js')
        .pipe(babel(babelOption))
        .pipe(gulp.dest('./es5/'));
});

gulp.task('esdoc', function () {
    gulp.src('./es6')
        .pipe(esdoc({
            "access": ["public", "protected", "private"],
            "autoPrivate": true,
            "destination": "./文档/esdoc",
            "index": "./文档/readme.md"
        }));
});

gulp.task("typedoc", function () {
    return gulp
        .src(["./ts/**/*.ts"])
        .pipe(typedoc({ 
            // TypeScript options (see typescript docs) 
            module: "commonjs",
            target: "es6",
            includeDeclarations: true,
            // Output options (see typedoc docs) 
            out: "./文档/doc",
            includes: './ts',
            json: "文档/doc/file.json",
            readme: "./文档/readme.md",
            // TypeDoc options (see typedoc docs) 
            // theme: 'minimal',
            
            name: "NodeJs串口通讯脚手架",
            ignoreCompilerErrors: false,
            version: true,
        }));
});



gulp.task('default', ['compile']);